package cn.kooki.app.duobao.ui.Activity.Goods;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.base.BaseNavActivity;
import cn.kooki.app.duobao.core.MainFactory;
import cn.kooki.app.duobao.data.Bean.base.BaseResponse;
import cn.kooki.app.duobao.data.Bean.detail.OtherItemRecord;
import cn.kooki.app.duobao.data.Bean.detail.OtherItemResponse;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.data.constant.Constants;
import cn.kooki.app.duobao.ui.Activity.Comm.RootAcitivity;
import cn.kooki.app.duobao.ui.Activity.User.UserCenterActivity;
import cn.kooki.app.duobao.ui.Adapter.OtherWinRecordAdapter;
import cn.kooki.app.duobao.ui.view.CustomRefreshHeader;
import cn.kooki.app.duobao.ui.view.LoadMoreContainer;
import cn.kooki.app.duobao.ui.view.LoadMoreHandler;
import cn.kooki.app.duobao.ui.view.LoadMoreListViewContainer;
import cn.kooki.app.duobao.ui.widget.ActionBarMenu;
import cn.kooki.app.duobao.ui.widget.CustomRelativeLayout;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.manager.AppManager;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import de.greenrobot.event.EventBus;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by xiong on 2015/12/30.
 */
public class OtherGoodWinRecordActivity extends BaseNavActivity implements View.OnClickListener {
    @Bind(R.id.list_view)
    ListView listView;
    @Bind(R.id.load_more_content_container)
    LoadMoreListViewContainer loadMoreContentContainer;
    @Bind(R.id.swipe_refresh_layout)
    PtrFrameLayout swipeRefreshLayout;
    @Bind(R.id.loading_wrapper)
    CustomRelativeLayout loadingWrapper;
    private String id;
    private int page = 1;
    private ArrayList<OtherItemRecord> datas = new ArrayList<>();
    private OtherWinRecordAdapter recordAdapter;
    private ActionBarMenu barMenu;

    @Override
    public void setLayoutContainerView() {
        setContainerView(R.layout.include_list_view_with_loadmore_fragment);
        ButterKnife.bind(this);

    }

    @Override
    protected void afterView() {
        setTitle("往期揭晓");
        id = getIntent().getStringExtra("id");
        int size = ShopCartManager.getInstance().getCartsSize();
        if (size > 0) {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow_withnew);
        } else {
            topRefresh.setImageResource(R.drawable.ic_menu_overflow);
        }
        topRefresh.setVisibility(View.VISIBLE);
        topRefresh.setOnClickListener(this);
        CustomRefreshHeader refreshHeader = new CustomRefreshHeader(this);
        swipeRefreshLayout.setHeaderView(refreshHeader);
        swipeRefreshLayout.addPtrUIHandler(refreshHeader);
        swipeRefreshLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, listView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getdata(false);
            }
        });
        loadingWrapper.setRetryClickListener(new CustomRelativeLayout.onRetryClickListener() {
            @Override
            public void onRetry() {
                loadingWrapper.hideNetError();
                swipeRefreshLayout.autoRefresh(false);
            }
        });
        loadingWrapper.showloading();
        swipeRefreshLayout.setVisibility(View.VISIBLE);
        loadMoreContentContainer.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                getdata(true);
            }
        });
        loadMoreContentContainer.useDefaultFooter();
        loadMoreContentContainer.setShowLoadingForFirstPage(true);
        recordAdapter = new OtherWinRecordAdapter(this, datas);
        recordAdapter.setOnItemClickListener(new OtherWinRecordAdapter.OnItemClickListener() {
            @Override
            public void onitemClick(int type, int position) {
                OtherItemRecord otherItemRecord = datas.get(position);
                if (type == 0) {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("id", otherItemRecord.s_id);
                    params.put("qishu", otherItemRecord.qishu);
                    intent(GoodsDetailAcitvity.class, params);
                } else {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("uid", otherItemRecord.q_uid);
                    intent(UserCenterActivity.class, params);
                }
            }
        });
        listView.setAdapter(recordAdapter);
        getdata(false);

    }

    private void getdata(final boolean isloadmore) {
        if (isloadmore) {
            page++;
        } else {
            page = 1;
        }
        MainFactory.getApiInstance().goodetailWinRecord(id, page, new Callback<OtherItemResponse>() {
            @Override
            public void success(OtherItemResponse baseResponse, Response response) {
                if (baseResponse.data != null) {
                    if (!isloadmore) {
                        datas.clear();
                    }
                    datas.addAll(baseResponse.data);
                    recordAdapter.notifyDataSetChanged();
                }
                loadMoreContentContainer.loadMoreFinish(datas.size() == 0, baseResponse.data.size() == 20);
                swipeRefreshLayout.refreshComplete();
                loadingWrapper.hideloading();
            }

            @Override
            public void failure(RetrofitError error) {
                if (error.getKind() == RetrofitError.Kind.NETWORK) {
                    loadingWrapper.showNetError();
                }
                swipeRefreshLayout.refreshComplete();
                loadingWrapper.hideloading();
                loadMoreContentContainer.loadMoreError(error.hashCode(), "网络错误,请重试");
            }
        });
    }

    private void showMenu() {
        if (barMenu == null) {
            barMenu = new ActionBarMenu(this);
            barMenu.setActionMenuListener(new ActionBarMenu.ActionBarMenuClickListener() {
                @Override
                public void onItemClick(int index) {
                    ChangePageEvent pageEvent = new ChangePageEvent();
                    if (index == 0) {
                        pageEvent.item = 3;
                    } else {
                        pageEvent.item = 0;
                    }
                    EventBus.getDefault().post(pageEvent);
                    AppManager.getAppManager().getTopWith(RootAcitivity.class);
                }
            });
        }
        barMenu.showAsDropDown(topRefresh, -ScreenUtils.dpToPxInt(this, 80), 0);
    }

    @Override
    public void onClick(View v) {
        if (v == topRefresh) {
            showMenu();
        }
    }
}
